package cn.com.headfree.technician.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.TOrgDept;
import cn.com.headfree.entity.TSeniorTechnician;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.technician.service.ISeniorService;

/**
 * 技能等级申报
 */
@Controller
@RequestMapping(value = "/technician", produces = "application/json;charset=UTF-8")
public class SeniorController extends ExBaseController {

	private static final Logger LOG = LogManager.getLogger(SeniorController.class);

	@Autowired
	private ISeniorService seniorService;

	@RequestMapping("/addSenior")
	@ResponseBody
	public ResultData addSenior(TSeniorTechnician senior, HttpServletRequest request) {
		ResultData rs = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			TSeniorTechnician entity = seniorService.judge(senior, user);
			if (entity != null&&!StringUtil.isNullOrEmpty(entity.getId())) {
				rs.setSuccess(false);
				rs.setErrorMsg("重复添加！");
			} else {
				// 添加技能等级申报
				seniorService.addSenior(senior, user);
				rs.setData("技能等级申报创建成功！");
				LOG.info("技能等级申报创建成功！");
			}
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("技能等级申报创建失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 分页查询技能等级申报
	 * 
	 * @param page
	 * @param tSeniorTechnician
	 * @return
	 */
	@RequestMapping("/getPageSeniorTechnician")
	@ResponseBody
	public ResultData getPageSeniorTechnician(Page<TSeniorTechnician> page, TSeniorTechnician tSeniorTechnician,
			HttpServletRequest request) {
		ResultData rs = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			seniorService.getPageSeniorTechnician(page, tSeniorTechnician, user);
			rs.setData(page);
			LOG.info("查找技能等级申报成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找技能等级申报失败！");
			e.printStackTrace();
		}
		return rs;
	}

	@RequestMapping("/addSeniorPre")
	@ResponseBody
	public ResultData addSeniorPre(HttpServletRequest request) {
		ResultData rs = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			TOrgDept dept = seniorService.addSeniorPre(user);
			Map<String, String> map = new HashMap<String, String>();
			map.put("userName", user.getName());
			map.put("orgName", dept.getName());
			map.put("erpNo", user.getFax());
			rs.setData(map);
			LOG.info("获取部门成功！");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("获取部门失败！");
			e.printStackTrace();
		}
		return rs;
	}

	@RequestMapping("/downloadAllFiles")
	public void downloadAllFiles(HttpServletRequest request, HttpServletResponse response, String sysCode,
			String businessNode,String seniorId) {
		ResultData rs = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			seniorService.downloadFiles(user.getFax(), sysCode, businessNode, request, response,seniorId);
			LOG.info("下载附件成功！");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("下载附件失败！");
			e.printStackTrace();
		}
	}

}
